package com.xinbo.tuiyousuper.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

import com.android.volley.VolleyError;
import com.baidu.location.i.f;
import com.google.gson.JsonObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinbo.tuiyousuper.R;
import com.xinbo.tuiyousuper.WebActivity;
import com.xinbo.tuiyousuper.fragment.my.MyFragmentBanner_fg3;
import com.xinbo.tuiyousuper.model.fg3data.banner.BannerData;
import com.xinbo.tuiyousuper.model.fg3data.banner.Datum;
import com.xinbo.tuiyousuper.model.fg3data.content.ContentData;
import com.xinbo.tuiyousuper.model.fg3data.content.Group;
import com.xinbo.tuiyousuper.model.fg3data.content.UserInfo;
import com.xinbo.tuiyousuper.model.fg3data.hot.Data;
import com.xinbo.tuiyousuper.model.fg3data.hot.GroupData;
import com.xinbo.tuiyousuper.model.fg3data.tags.Tag;
import com.xinbo.tuiyousuper.model.fg3data.tags.TagsData;
import com.xinbo.tuiyousuper.utils.ApiClient;
import com.xinbo.tuiyousuper.utils.CircleImageView;
import com.xinbo.tuiyousuper.utils.CustomerGridView;
import com.xinbo.tuiyousuper.utils.CustomerListView;
import com.xinbo.tuiyousuper.utils.ViewPagerIndicator;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.UILUtils;
import com.xinbo.utils.VolleyListener;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class Fragment3 extends Fragment
{
	private LayoutInflater inflater;
	private ViewPager mPager;
	private List<Datum> bannerList = new ArrayList<Datum>();
	private MoreGroupAdapter moreGroupAdapter;
	private List<com.xinbo.tuiyousuper.model.fg3data.hot.List> list = new ArrayList<com.xinbo.tuiyousuper.model.fg3data.hot.List>();
	private List<com.xinbo.tuiyousuper.model.fg3data.tags.Datum> data = new ArrayList<com.xinbo.tuiyousuper.model.fg3data.tags.Datum>();
	private TitilAdapter titilAdapter;
	private CustomerGridView gvTitle;
	private CustomerGridView gvContent;
	private List<Tag> tags = new ArrayList<Tag>();
	private ContextAdapter contextAdapter;
	private List<com.xinbo.tuiyousuper.model.fg3data.hot.List> hotList = new ArrayList<com.xinbo.tuiyousuper.model.fg3data.hot.List>();
	private CustomerGridView gvHotGroup;
	private DisplayMetrics dm = new DisplayMetrics();
	private int index = 0;
	private DetailAdapter detailAdapter;
	private List<com.xinbo.tuiyousuper.model.fg3data.content.List> contentList= new ArrayList<com.xinbo.tuiyousuper.model.fg3data.content.List>();
	private ViewPagerIndicator mIndicator;
	private boolean userDrag;
	private boolean needAuto;
	private Runnable r;
	public Fragment3()
	{
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState)
	{
		
		needAuto=true;
		this.inflater = inflater;
		View layout = inflater.inflate(R.layout.fragment_fragment3, container,
		        false);
		mPager = (ViewPager) layout.findViewById(R.id.ViewPager1_fg3);
		gvHotGroup = (CustomerGridView) layout
		        .findViewById(R.id.hotgroupGridView_fg3);
		gvTitle = (CustomerGridView) layout
		        .findViewById(R.id.titleGridView_fg3);
		gvContent = (CustomerGridView) layout
		        .findViewById(R.id.contentGridView_fg3);
		CustomerListView mListView = (CustomerListView) layout
		        .findViewById(R.id.contentListView_fg3);
		mIndicator = (ViewPagerIndicator) layout.findViewById(R.id.viewPagerIndicator_fg3);
		moreGroupAdapter = new MoreGroupAdapter();
		titilAdapter = new TitilAdapter();
		contextAdapter = new ContextAdapter();
		detailAdapter = new DetailAdapter();
		gvHotGroup.setAdapter(moreGroupAdapter);
		gvTitle.setAdapter(titilAdapter);
		gvContent.setAdapter(contextAdapter);
		mListView.setAdapter(detailAdapter);
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		initData();
		gvTitle.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
		gvContent.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
		gvTitle.performItemClick(gvTitle, 0, 0);
		gvContent.performItemClick(gvContent,0, 0);
		gvTitle.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
	            index=position;
	            titilAdapter.notifyDataSetChanged();
            }
		});
		gvContent.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
					if (index==0&&position==1)
                    {
	                  
                    }else if(index==1&&position==0){
						
					}else if(index==2&&position==0)
					{
						
					}else {
						 initDetial();
					}
					
            }
		});
		return layout;
	}

	protected void initShare()
    {
		OnekeyShare oks = new OnekeyShare();
		oks.setAddress("12345678901");
		oks.setTitle(getString(R.string.ssdk_oks_share));
		oks.setTitleUrl("http://mob.com");
		oks.setText("");
		oks.setUrl("http://www.mob.com");
		oks.setComment(getString(R.string.ssdk_oks_share));
		oks.setSite(getString(R.string.app_name));
		oks.setSiteUrl("http://mob.com");
		oks.setVenueName("ShareSDK");
		oks.setVenueDescription("This is a beautiful place!");
		oks.disableSSOWhenAuthorize();
		oks.show(getContext());
    }

	private void initData()
	{
		ApiClient.fg3FinderBanner(getContext(), new VolleyListener()
		{

			@Override
			public void onResponse(String json)
			{
				BannerData bannerData = GsonUtils.parseJSON(json,
				        BannerData.class);
				bannerList = bannerData.getData();
				setPagerAdapter(bannerList);
			}

			@Override
			public void onErrorResponse(VolleyError arg0)
			{
			}
		});
		ApiClient.fg3FinderTitle(getContext(), new VolleyListener()
		{

			@Override
			public void onResponse(String json)
			{
				TagsData tagsData = GsonUtils.parseJSON(json, TagsData.class);
				data = tagsData.getData();
				gvTitle.setNumColumns(data.size());
				titilAdapter.notifyDataSetChanged();
			}

			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				// TODO Auto-generated method stub

			}
		});
		ApiClient.fg3MoreGroup(getContext(), new VolleyListener()
		{

			@Override
			public void onResponse(String json)
			{
				GroupData groupData = GsonUtils
				        .parseJSON(json, GroupData.class);
				Data hotData = groupData.getData();
				hotList = hotData.getList();
				LayoutParams layoutParams = gvHotGroup.getLayoutParams();
				layoutParams.width = dm.widthPixels / 3 * hotList.size();
				gvHotGroup.setLayoutParams(layoutParams);
				gvHotGroup.setStretchMode(GridView.NO_STRETCH);
				gvHotGroup.setColumnWidth(dm.widthPixels / 3);
				gvHotGroup.setNumColumns(hotList.size());
				moreGroupAdapter.notifyDataSetChanged();
			}

			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				// TODO Auto-generated method stub

			}
		});
		initDetial();
	}

	private void initDetial()
    {
	    ApiClient.fg3FinderDetail(getContext(), new VolleyListener()
		{
			@Override
			public void onResponse(String json)
			{
				ContentData contentData = GsonUtils.parseJSON(json, ContentData.class);
				com.xinbo.tuiyousuper.model.fg3data.content.Data data2 = contentData.getData();
				contentList = data2.getList();
				detailAdapter.notifyDataSetChanged();
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0)
			{
			}
		});
    }

	/** 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 */
	class DetailAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return contentList.size();
		}

		@Override
		public Object getItem(int position)
		{
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(R.layout.content_listview_fg3_item, null);
			//头锟斤拷
			CircleImageView ivHeadImg = (CircleImageView) layout.findViewById(R.id.circleiv_headimg_listview_fg3);
			//锟角筹拷
			TextView tvNickNAME = (TextView) layout.findViewById(R.id.tv_nickname);
			//时锟斤拷
			TextView tvTime = (TextView) layout.findViewById(R.id.tv_time_listview_fg3);
			//锟脚讹拷
			TextView tvGroupName = (TextView) layout.findViewById(R.id.tv_groupname_listview_fg3);
			//锟斤拷锟斤拷
			TextView tvTitle = (TextView) layout.findViewById(R.id.tv_title_listview_fg3);
			//锟斤拷锟斤拷
			TextView tvText= (TextView) layout.findViewById(R.id.tv_text_listview_fg3);
			//锟斤拷图
			ImageView ivImage = (ImageView) layout.findViewById(R.id.iv_activityImgUrl_listview_fg3);
			//锟斤拷签
			TextView tvTag = (TextView) layout.findViewById(R.id.tv_tagname_listview_fg3);
			//锟饺讹拷
			TextView tvHotIndex = (TextView) layout.findViewById(R.id.tv_hotindex_listview_fg3);
			//锟斤拷锟斤拷锟斤拷锟斤拷
			TextView tvPraiseNum= (TextView) layout.findViewById(R.id.tv_praiseCnt_listview_fg3);
			//分享
			TextView tvShare=(TextView) layout.findViewById(R.id.tv_share_listview_fg3);
			com.xinbo.tuiyousuper.model.fg3data.content.List content = contentList.get(position);
			UserInfo userInfo = content.getUserInfo();
			final String h5Url = content.getH5Url();
			String time = content.getTime();
			String headImage = userInfo.getHeadImage();
			String nickName = userInfo.getNickName();
			Group group = content.getGroup();
			Integer hotIndex = content.getHotIndex();
			List<Tag> tags2 = content.getTags();
			String activityImgUrl = content.getActivityImgUrl();
			Integer praiseCnt = content.getPraiseCnt();
			//String tagName = tags2.get(0).getTagName();
			String title = content.getTitle();
			String text = content.getText();
			String groupName = group.getGroupName();
			if (!"".equals(headImage))
			{
				UILUtils.displayImageNoAnim(headImage, ivHeadImg);
			}
			tvNickNAME.setText(nickName);
			tvTime.setText(time);
			tvGroupName.setText(groupName);
			tvTitle.setText(title);
			tvText.setText(text);
			//tvTag.setText(tagName);
			tvHotIndex.setText(hotIndex+" 热度");
			tvPraiseNum.setText(praiseCnt+"");
			UILUtils.displayImageNoAnim(activityImgUrl, ivImage);
			tvShare.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					initShare();
					
				}
			});
			ivImage.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent();
					intent.putExtra("fg3", h5Url);
					intent.setClass(getContext(), WebActivity.class);
					startActivityForResult(intent, 0);
				}
			});
			return layout;
		}

	}

	/** 锟斤拷锟斤拷锟剿碉拷锟斤拷锟斤拷锟斤拷 */
	class ContextAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return tags.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(R.layout.title_gridview_fg3_item,
			        null);
			TextView tvTitle = (TextView) layout
			        .findViewById(R.id.tvtitle_fg3);
			Tag tag = tags.get(position);
			tvTitle.setText(tag.getTagName());
			return layout;
		}

	}

	/** 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 */
	class TitilAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(R.layout.title_gridview_fg3_item,
			        null);
			TextView tvTitle = (TextView) layout
			        .findViewById(R.id.tvtitle_fg3);
			com.xinbo.tuiyousuper.model.fg3data.tags.Datum datum = data
			        .get(position);
			String tagCategoryName = datum.getTagCategoryName();
			tvTitle.setText(tagCategoryName);
			tags = data.get(index).getTags();
			gvContent.setNumColumns(tags.size());
			contextAdapter.notifyDataSetChanged();
			return layout;
		}

	}

	/** 锟斤拷锟叫★拷锟斤拷萍锟斤拷锟斤拷锟斤拷锟�*/
	class MoreGroupAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return hotList.size();
		}

		@Override
		public Object getItem(int position)
		{
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(
			        R.layout.moregroup_gridview_fg3_item, null);
			TextView tvTitil = (TextView) layout
			        .findViewById(R.id.tv_moregroup_fg3);
			ImageView mImageView = (ImageView) layout
			        .findViewById(R.id.iv_moregroup_fg3);
			com.xinbo.tuiyousuper.model.fg3data.hot.List baseList = hotList
			        .get(position);
			final String h5Url = baseList.getH5Url();
			mImageView.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent();
					intent.putExtra("fg3", h5Url);
					intent.setClass(getContext(), WebActivity.class);
					startActivityForResult(intent, 0);
				}
			});
			tvTitil.setText(baseList.getGroupName());
			UILUtils.displayImageNoAnim(baseList.getImgUrl(), mImageView);
			return layout;
		}

	}

	protected void setPagerAdapter(final List<Datum> bannerList)
	{
		FragmentManager fm = getChildFragmentManager();
		mPager.setAdapter(new FragmentPagerAdapter(fm)
		{

			@Override
			public int getCount()
			{

				return 100000;
			}

			@Override
			public Fragment getItem(int position)
			{
				int index = position % bannerList.size();
				mIndicator.setCount(bannerList.size());
				return new MyFragmentBanner_fg3(bannerList, index);
			}
		});
		mPager.addOnPageChangeListener(new OnPageChangeListener()
		{
			
			@Override
			public void onPageSelected(int arg0)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrolled(int position, float arg1, int arg2)
			{
				mIndicator.move(arg1, position % 5);
				
			}
			
			@Override
			public void onPageScrollStateChanged(int state)
			{
				switch (state)
				{
				// 锟矫伙拷锟斤拷锟斤拷时状态
				case ViewPager.SCROLL_STATE_DRAGGING:
					userDrag = true;
					break;
				// 锟斤拷锟斤拷状态
				case ViewPager.SCROLL_STATE_IDLE:
					userDrag = false;
					break;
				// 锟斤拷锟斤拷状态
				case ViewPager.SCROLL_STATE_SETTLING:
					userDrag = false;
					break;
				default:
					break;
				}
				
			}
		});
		mPager.removeCallbacks(r);
		r = new Runnable()
		{
			
			@Override
			public void run()
			{
				if (!userDrag && needAuto)
				{
					// 锟斤拷玫锟角癡iewPager锟斤拷页锟斤拷然锟斤拷锟�锟斤拷锟斤拷
					int nextItem = mPager.getCurrentItem() + 1;
					// 锟斤拷锟斤拷锟斤拷一页为锟斤拷前页锟斤拷锟斤拷锟斤拷时3锟诫触锟斤拷
					mPager.setCurrentItem(nextItem);
				}
			}
		};
		mPager.postDelayed(r, 5000);
	}

	@Override
	public void onDestroyView()
	{
		ImageLoader.getInstance().clearMemoryCache();
		needAuto=false;
		super.onDestroyView();
	}
}
